﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;

namespace iPark.Core.Storage
{
    public class Storage : IStorage
    {
        public Storage() { }

        public List<StorageData> QueryAllParkingData()
        {
            List<StorageData> result = null;

            using (StorageContext db = new StorageContext(StorageContext.connectingString))
            {
                var query = from parkingInfo in db.StorageItems
                            select parkingInfo;

                result = query.ToList();
            }

            return result;
        }

        public void CreateDatabase()
        {
            using (StorageContext db = new StorageContext(StorageContext.connectingString))
            {
                if (db.DatabaseExists())
                {
                    db.DeleteDatabase();
                }

                db.CreateDatabase();
            }
        }

        public void InsertData(StorageData data)
        {
            using (StorageContext db = new StorageContext(StorageContext.connectingString))
            {
                db.StorageItems.InsertOnSubmit(data);

                db.SubmitChanges();
            }
        }

        public List<StorageData> QueryDataByRadius(double longitude, double latitude, double radius)
        {
            List<StorageData> results = null;

            float xMin, xMax, yMin, yMax;

            float dist = (float)(radius / 100000.0);

            xMin = (float)longitude - dist;

            xMax = (float)longitude + dist;

            yMin = (float)latitude - dist;

            yMax = (float)latitude + dist;

            using (StorageContext db = new StorageContext(StorageContext.connectingString))
            {
                var query = from parkingInfo in db.StorageItems
                            where parkingInfo.LongitudeBaidu >= xMin && parkingInfo.LongitudeBaidu <= xMax
                            && parkingInfo.LatitudeBaidu >= yMin && parkingInfo.LatitudeBaidu <= yMax
                            select parkingInfo;

                results = query.ToList();
            }

            return results;
        }

        public List<StorageData> QueryNameLike(string searchName)
        {
            List<StorageData> results = null;

            string lowerSearch = searchName.ToLower();

            using (StorageContext db = new StorageContext(StorageContext.connectingString))
            {
                var query = from parkingInfo in db.StorageItems
                            where parkingInfo.FullName.ToLower().Contains(lowerSearch) ||
                                parkingInfo.ShortName1.ToLower().Contains(lowerSearch) ||
                                parkingInfo.ShortName2.ToLower().Contains(lowerSearch) ||
                                parkingInfo.ShortName3.ToLower().Contains(lowerSearch)
                            select parkingInfo;

                if (query.Count() > 0)
                {
                    results = query.ToList();
                }
            }

            return results;
        }
    }
}
